package cn.exrick.xboot.modules.research.controller;

import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;

import cn.exrick.xboot.modules.research.entity.Administrator;
import cn.exrick.xboot.modules.research.service.IAdministratorService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Column;
import java.util.List;

//import static net.sf.jsqlparser.util.validation.metadata.NamedObject.column;

/**
 * @author lty
 */
@Slf4j
@RestController
@Api(tags = "管理员管理接口")
@RequestMapping("/xboot/administrator")
@Transactional
public class AdministratorController {

    @Autowired
    private IAdministratorService iAdministratorService;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<Administrator> get(@PathVariable String id) {

        Administrator administrator = iAdministratorService.getById(id);
        return new ResultUtil<Administrator>().setData(administrator);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<Administrator>> getAll() {

        List<Administrator> list = iAdministratorService.list();
        return new ResultUtil<List<Administrator>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<Administrator>> getByPage(@ModelAttribute Administrator administrator,@ModelAttribute PageVo page) {
        QueryWrapper<Administrator> qw =new QueryWrapper<>();
        if (StrUtil.isNotBlank(administrator.getAdministratorId())){
            qw.like("administrator_Id",administrator.getAdministratorId());
        }

        if (StrUtil.isNotBlank(administrator.getName())){
            qw.like("name",administrator.getName());
        }
        if (StrUtil.isNotBlank(administrator.getSex())){
            qw.like("sex",administrator.getSex());
        }
        if (StrUtil.isNotBlank(administrator.getNickname())){
            qw.like("nickname",administrator.getNickname());
        }
        if (StrUtil.isNotBlank(administrator.getMobile())){
            qw.like("mobile",administrator.getMobile());
        }
        if (StrUtil.isNotBlank(administrator.getDepartmentName())){
            qw.like("department_Name",administrator.getDepartmentName());
        }
        if (StrUtil.isNotBlank(administrator.getType())){
            qw.like("type",administrator.getType());
        }

        IPage<Administrator> data = iAdministratorService.page(PageUtil.initMpPage(page),qw);
        return new ResultUtil<IPage<Administrator>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<Administrator> saveOrUpdate(Administrator administrator) {

        if (iAdministratorService.saveOrUpdate(administrator)) {
            return new ResultUtil<Administrator>().setData(administrator);
        }
        return new ResultUtil<Administrator>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@RequestParam String[] ids) {

        for (String id : ids) {
            iAdministratorService.removeById(id);
        }
        return ResultUtil.success("批量通过id删除数据成功");
    }
}
